package org.eae.eldf.webapp.controller.prod;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eae.eldf.webapp.base.AbstractController;
import org.eae.eldf.dto.module.ModuleRolePurviewDto;
import org.eae.eldf.entity.module.ModuleMenu;
import org.eae.eldf.entity.module.ModulePurview;
import org.eae.eldf.entity.module.ModuleRolePurview;
import org.eae.eldf.entity.prod.ProdCategory;
import org.eae.eldf.entity.prod.ProdCategoryPurview;
import org.eae.eldf.framework.json.JSONArray;
import org.eae.eldf.framework.persistence.Criteria;
import org.eae.eldf.framework.spring.SpringSecurityUtils;
import org.eae.eldf.service.module.ModuleMenuService;
import org.eae.eldf.service.module.ModulePurviewService;
import org.eae.eldf.service.prod.ProdCategoryPurviewService;
import org.eae.eldf.service.prod.ProdCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;


/**
 * 产品类别权限控制层
 * @author xl
 *
 */
@Controller
@RequestMapping("/prod")
public class ProdCategoryPurviewController extends AbstractController<ProdCategoryPurview> {

	
	
	private ProdCategoryService prodCategoryService;

	private ProdCategoryPurviewService prodCatecopyPurviewService;

	private ModuleMenuService moduleMenuService;

	private ModulePurviewService modulePurviewService;
	


	@Autowired
	public void setProdCatecopyPurviewService(
			ProdCategoryPurviewService prodCatecopyPurviewService) {
		this.prodCatecopyPurviewService = prodCatecopyPurviewService;
	}

	@Autowired
	public void setModulePurviewService(ModulePurviewService modulePurviewService) {
		this.modulePurviewService = modulePurviewService;
	}

	@Autowired
	public void setModuleMenuService(ModuleMenuService moduleMenuService) {
		this.moduleMenuService = moduleMenuService;
	}
	
	@Autowired
	public void setProdCategoryService(ProdCategoryService prodCategoryService) {
		this.prodCategoryService = prodCategoryService;
	}
	
	@RequestMapping(value = "/purview/list")
	public String getRolePurviewTree(@RequestParam(required = false) Long id,
			ModelMap model, HttpServletRequest request,
			HttpServletResponse response) {
		
		ProdCategory prodCategory = prodCategoryService.get(ProdCategory.class, id);
		
		List<ModuleRolePurviewDto> list = new ArrayList<ModuleRolePurviewDto>();
		ModuleRolePurviewDto moduleRolePurviewDto = null;
		
		List<ModuleMenu>  menuList=moduleMenuService.queryList(Criteria.create(ModuleMenu.class));
		for (ModuleMenu moduleMenu : menuList) {
			moduleRolePurviewDto = new ModuleRolePurviewDto();
			moduleRolePurviewDto.setPId("mu_" + moduleMenu.getModuleMenuId());
			moduleRolePurviewDto.setName(moduleMenu.getMenuName());
			moduleRolePurviewDto.setParentId("mu_" + moduleMenu.getParentId());
			list.add(moduleRolePurviewDto);
		}
		
		List<ModulePurview> purList = modulePurviewService.queryByMenuList(menuList);
		List<ProdCategoryPurview> prodPurList=prodCatecopyPurviewService.queryList(Criteria.create(ProdCategoryPurview.class).where("prodCategoryId", new Object[]{prodCategory.getProdCategoryId()}));
		for (ModulePurview modulePurview : purList) {
			moduleRolePurviewDto = new ModuleRolePurviewDto();
			moduleRolePurviewDto.setPId("pur_" + modulePurview.getModulePurviewId());
			moduleRolePurviewDto.setName(modulePurview.getPurName());
			moduleRolePurviewDto.setParentId("mu_" + modulePurview.getModuleMenuId());
			for (ProdCategoryPurview prodCategoryPurview : prodPurList) {
				if (prodCategoryPurview.getModulePurviewId().equals(modulePurview.getModulePurviewId()) ) {
					moduleRolePurviewDto.setIsChecked(true);
					continue;
				}
			}
			list.add(moduleRolePurviewDto);
		}
		
		model.addAttribute("list", new JSONArray(list));
		model.addAttribute("prodCategory", prodCategory);
		return "/prod/category/purview";
	}
	
	/**
	 * 模块权限插入
	 * 
	 * @param id
	 *            角色ID
	 * @param listPid
	 *            权限ID列表
	 * @param modelMap
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/purview/save")
	public String saveOrEdit(@RequestParam(required = false) Long id,
			ModelAndView modelAndView,
			@RequestParam(required = false) String listPid,
			HttpServletRequest request,HttpServletResponse response) {
		
		// 删除角色菜单权限
		prodCatecopyPurviewService.delete( Criteria.create(ProdCategoryPurview.class)
				.where("prodCategoryId", new Object[] { id }));
		// 重新给角色添加菜单权限
		String[] pIds = listPid.split("\\^");
		if (!listPid.equals("")) {
			for (String pid : pIds) {
				long modulePurviewId = Long.parseLong(pid);
				prodCatecopyPurviewService.insert(Criteria
						.create(ProdCategoryPurview.class)
						.set("prodCategoryId", id)
						.set("modulePurviewId", modulePurviewId)
						.set("lastUpdateTime", new Date())
						.set("lastUpdateUser",
								SpringSecurityUtils.getCurrentUserName())
						.set("createTime", new Date())
						.set("createUser",
								SpringSecurityUtils.getCurrentUserName()));
			}
		}
		return "/prod/category/list";
	}
}
